<?php

/***************************************************************\
* FILE:		sys/pages/gallery.inc
* SECTION:	pages > items
* FUNCTION:	display a gallery full of pics
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/itemfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	item_page_inc_check_access();
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		default:
			item_page_inc_execute_action($action, 'article');
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;
	$CONTENT = array();

	$where = "ITEM.item_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.item_id = GALLERY.item_id AND \n\t";
	$where .= "ITEM.author = M1.user_id AND \n\t";
	$where .= "ITEM.editor = M2.user_id";

	$sql = db_query_select(array(array('*'),array('*'),array(),"M1.nick AS nick_author, M2.nick AS nick_editor"), array('ITEM','GALLERY','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), $where);
	$sqlresult = db_do_query($sql);

	if (db_num_rows($sqlresult) !=1)
		errorpage(404);

	$CONTENT['gallery'] = db_fetch_array($sqlresult);

	if ($CONTENT['gallery']['deleted'])
		errorpage(404);
	
	if ($CONTENT['gallery']['open_for_all'] or ($CONTENT['gallery']['author'] == $_SESSION['user_id']))
		add_action('add_pic', 'popup', "{$PAGE['id']}.art?action=add", '', 1);
	
	$sql = db_query_select(array(array(), "MEMBER.nick, ART.*"), array("ART INNER JOIN MEMBER ON ART.artist = MEMBER.user_id"), "item_id = {$PAGE[id]}", "added ASC");
	$sqlresult = db_do_query($sql);
	
	$CONTENT['gallery']['pics'] = array();
	$rights = has_access("MODERATE") or has_access("ADMIN");
	while ($art = db_fetch_array($sqlresult)) {
		if ($rights or ($art['artist'] == $_SESSION['user_id']))
			$art['edit'] = "javascript:popup('{$art['art_id']}.art?action=edit',400,300);";
		array_unshift($CONTENT['gallery']['pics'], $art);
	}
	
	$CONTENT['title'] = $CONTENT['gallery']['title'];
	$CONTENT['owner'] = $CONTENT['gallery']['author'];
		
	$PAGE['content'] = $CONTENT;
	
	item_page_inc_execute('gallery');
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	item_page_inc_dress_before();
	print_gallery($PAGE['content']['gallery']);
	item_page_inc_dress_after();
}

?>
